import FcDesigner from '@form-create/designer';
import { Notification, MessageBox, Message, Loading } from 'element-ui'
import uniqueId from '@form-create/utils/lib/unique';
//import genSignature from '../../../utils/PostObject.js';
const filename1 = uniqueId();
const filename2 = new Date().getTime();
const label = '上传';
const name = 'upload';
var filename = uniqueId()+"png";
import '../../../utils/crypto1/crypto/crypto'
import '../../../utils/crypto1/hmac/hmac'
import '../../../utils/crypto1/sha1/sha1'
import { Base64 } from 'js-base64'

/**
 * 生成policy和签名
 * 默认1小时后失效
 * @param {*} accessKeySecret oss accessKeySecret
 * @param {*} expiration 过期时间 单位秒
 * @returns
 */
function genSignature () {
  const accessKeySecret = 'ZNVPLxgMYGa7CZsghYOVnyOyyJOGFv';
  const expiration = (new Date(Date.now() +  1200 * 1000)).toISOString()

  const policy = {
    expiration: expiration, // 失效时间
    conditions: [
      ['content-length-range', 0, 469824028] // 设置上传文件的大小限制 1G
    ]
  }
  const policyBase64 = Base64.encode(JSON.stringify(policy))

  const bytes = Crypto.HMAC(Crypto.SHA1, policyBase64, accessKeySecret, { asBytes: true });
  const signature = Crypto.util.bytesToBase64(bytes);

  return {
    OSSAccessKeyId:'LTAI5tEewCaBy7zsdcL6GmVT',
    policy:policyBase64,
    key:filename,
    success_action_status:'200',
    signature
  }
}

export default {
  icon: 'icon-upload',
  label,
  name,
  rule() {
    return {
      type: name,
      field: uniqueId(),
      title: label,
      info: '',
      $required: true,
      on:{
        'before-upload':function () {
          console.log("进入aaaaa")
          //filename = file.name;
        }
      },
      props: {
        action:"https://hncxsk.oss-cn-shenzhen.aliyuncs.com/",
        uploadType:'image',
        data: genSignature(filename),

        onSuccess(res, file) {
          file.url = "https://img.cscxsk.cn/"+filename;
        },
        limit:1,
      }
    };
  },
  props() {
    return [ FcDesigner.makeOptionsRule('options'),
      {
        type: "Required",
        field: "formCreate$required",
        title: "是否必填",
        value: true
      },
      {
        type: 'select',
        field: 'uploadType',
        title: '上传类型',
        value: 'image',
        options: [{label: '图片', value: 'image'}, {
          label: '文件',
          value: 'file'
        }]
      }, {type: 'input', field: 'action', title: '上传的地址(必填)'}, {
        type: 'Struct',
        field: 'headers',
        title: '设置上传的请求头部',
        props: {defaultValue: {}}
      }, {type: 'switch', field: 'multiple', title: '是否支持多选文件'}, {
        type: 'Struct',
        field: 'data',
        title: '上传时附带的额外参数',
        props: {defaultValue: {}}
      }, {type: 'input', field: 'name', title: '上传的文件字段名'}, {
        type: 'switch',
        field: 'withCredentials',
        title: '支持发送 cookie 凭证信息'
      }, {type: 'input', field: 'accept', title: '接受上传的文件类型（thumbnail-mode 模式下此参数无效）'}, {
        type: 'switch',
        field: 'autoUpload',
        title: '是否在选取文件后立即进行上传',
        value: true
      }, {
        type: 'switch',
        field: 'disabled',
        title: '是否禁用'
      }, {
        type: 'inputNumber',
        field: 'limit',
        title: '最大允许上传个数',
        props: {min: 0},
      }];
  }
};
